package com.ansion.basedb.storage;

import android.content.SharedPreferences;

import com.ansion.basedb.storage.api.BaseKeyValueStorage;
import com.ansion.basedb.storage.api.MMKVStorage;
import com.ansion.log.VLog;
import com.ansion.preference.TurboBasePreference;
import com.ansion.utils.ResourceUtils;


/***
 * @author john 2022/06/21
 * @desc 通用数据升级管理
 * 主要包含以下能力：
 * 1.懒加载升级sp文件，只有在获取sp文件的时候才会对sp文件做升级。
 * 为什么要采用懒加载的形式，是因为mmkv升级很快，通常几个毫秒就搞定，而sp文件的读取是最耗时的
 *
 */
public class SharedPreferencesManager {
    private static volatile SharedPreferencesManager inst;
    private BaseKeyValueStorage confKvs;

    private SharedPreferencesManager() {
    }

    public static SharedPreferencesManager getInst() {
        if (inst == null) {
            synchronized (SharedPreferencesManager.class) {
                if (inst == null)
                    inst = new SharedPreferencesManager();
            }
        }
        return inst;
    }

    private final String CONF_NAME = "DataUpdate.name";
    //    private final String KEY_VERSION_COMMON = "DataUpdate.version.common";
//    private final String KEY_VERSION_USER = "DataUpdate.version.user";
    private final String KEY_UPDATE_RECORD_PREFIX = "DataUpdate.record";
//    private final int VERSION = SharedPreferenceConfig.VERSION;


//    private void updateCommonData0_1() {
//        update2MMKV(SharedPreferenceConfig.PREF_APK_URL);
//        update2MMKV(SharedPreferenceConfig.PEEF_SETTINGS_ME);
//        update2MMKV(SharedPreferenceConfig.PREF_VERIFYPHONE_INFO);
//        update2MMKV(SharedPreferenceConfig.PREF_VERSION_NAME);
//        update2MMKV(SharedPreferenceConfig.PREF_OFFICIAL_ACCOUNT);
//        update2MMKV(SharedPreferenceConfig.PREF_RPC_REQ);
//        update2MMKV(SharedPreferenceConfig.PREF_MAIN_TAB_LOAD_PROCESS);
//    }
//
//    private void updateUserData0_1() {
//        //uid绑定的sp需要专门的标记
//        if (ContactUtil.getMyUid() > 0)
//            update2MMKV(SharedPreferenceConfig.getFormatUserPrefName());
//    }

    private MMKVStorage update2MMKV(String name) {
        MMKVStorage storage = StorageSdk.getMMKVStorage(name, true);
        VLog.i("TAG_DATA_UPDATE", "update2MMKV start for %s", name);
        long start = System.currentTimeMillis();
        SharedPreferences sp = ResourceUtils.getSharedPreferences(name);
        long cost = System.currentTimeMillis() - start;
        VLog.i("TAG_DATA_UPDATE", "update2MMKV read sp %s cost:%d", name, cost);
        start = System.currentTimeMillis();
        storage.importFromSharedPreferences(sp);
        cost = System.currentTimeMillis() - start;
        VLog.i("TAG_DATA_UPDATE", "update2MMKV importFromSharedPreferences success %s cost:%d", name, cost);
        getConfKvs().putBoolean(KEY_UPDATE_RECORD_PREFIX + name, true);
        VLog.i("TAG_DATA_UPDATE", "update2MMKV success for %s", name);
        return storage;
    }


    /***
     * 采用懒加载异步进行sp往mmkv升级
     * @param name
     * @return
     */
    public synchronized SharedPreferences getMMKVSharedPreferences(String name) {
        long start = System.currentTimeMillis();
        SharedPreferences sp = getMMKVSharedPreferencesImpl(name);
        long cost = System.currentTimeMillis() - start;
        if (VLog.OPEN_LOG && !name.contains("FormatUserid_") && !name.contains("User_")) {
            VLog.i("TAG_DATA_UPDATE", "getMMKVSharedPreferencesImpl for %s cost %d ms ", name, cost);
        }
        return sp;
    }

    private SharedPreferences getMMKVSharedPreferencesImpl(String name) {
        if (!TurboBasePreference.MMKVEnable) {
            return ResourceUtils.getSharedPreferences(name);
        }

        boolean hasUpdate = getConfKvs().contains(KEY_UPDATE_RECORD_PREFIX + name);
        if (hasUpdate) {
            boolean updateSuccess = getConfKvs().getBoolean(KEY_UPDATE_RECORD_PREFIX + name, false);
            if (updateSuccess) {
                MMKVStorage kvs = StorageSdk.getMMKVStorage(name, true);
                return kvs.sharePreference();
            }

        } else {//update
            MMKVStorage storage = update2MMKV(name);
            return storage.sharePreference();

        }
        return ResourceUtils.getSharedPreferences(name);
    }

    private synchronized BaseKeyValueStorage getConfKvs() {
        if (confKvs == null) {
            confKvs = StorageSdk.getStorage(CONF_NAME);
        }
        return confKvs;
    }


}
